# ============================================================
# * Copyright © hk. 2022-2025. All rights reserved.
# * File name: CMakeLists.txt
# * Author   : 苏木
# * Date     : 2025-05-22
# * Version  : 
# * Description: cmake version 3.16.3
# ============================================================
## https://cmake.com.cn/cmake/help/latest/guide/tutorial/index.html
## https://cmake.org/cmake/help/latest/

# 指定 CMake 的最低版本要求
# --------------------------------------------------
cmake_minimum_required(VERSION 3.10)


# 设置项目名称和使用的编程语言
# --------------------------------------------------
project(SPROJECT C)
set(app_target "app_demo.out")

# 启用/禁用在生成期间输出编译命令。会生成compile_commands.json文件,包含编译命令，可以被clangd使用实现跳转和补全
# --------------------------------------------------
# 注意,这个选项放在PROJECT之前的话可能导致第一次构建不生效
# SET(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Enable compile commands export" FORCE) # 强制设置变量并更新缓存
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)


# 设置C编译器
# --------------------------------------------------
# set(CMAKE_C_COMPILER "/home/sumu/2software/gcc-arm-linux-gnueabihf-8.3.0/bin/arm-linux-gnueabihf-gcc")
set(CMAKE_C_COMPILER "gcc")
message(STATUS "[USER:TOP]CMAKE_C_COMPILER : ${CMAKE_C_COMPILER}") # c编译器，一般默认是gcc


# 设置C编译选项,要放在 add_executable 之前
# --------------------------------------------------
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
add_compile_options(-g -Wall) # 不会去修改 CMAKE_C_FLAGS，一般还是建议用这个


# 指定可执行程序和源文件
# --------------------------------------------------
include_directories(func_version)           # 添加头文件目录
add_subdirectory(func_version)              # 添加子目录（会自动执行子目录的CMakeLists.txt）

set(src_list main.c)                        # 设置main函数源文件
add_executable(${app_target} ${src_list})

target_link_libraries(${app_target} func_version_static) # 链接静态库，这里使用静态库目标名称，而不是输出名称
# target_link_libraries(${app_target} func_version_shared) # 链接动态库，这里使用动态库目标名称，而不是输出名称

# 获取目标的属性,要先指定目标才能获取
# --------------------------------------------------
get_target_property(app_compile_options app_demo.out COMPILE_OPTIONS) # 获取编译选项属性
message(STATUS "[USER:TOP]Target Compile Options: ${app_compile_options}")
message(STATUS "[USER:TOP]CMAKE_C_FLAGS         : ${CMAKE_C_FLAGS}")      # c编译选项

# install: cmake -DCMAKE_INSTALL_PREFIX=/usr
# --------------------------------------------------
# 1. 安装目标文件
install(TARGETS ${app_target} func_version_shared func_version_static
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION libstatic
)
# 2. 普通文件的安装
file(GLOB_RECURSE lib_header_files 
    LIST_DIRECTORIES false
    "func_version/*.h"
)
install(FILES COPYRIGHT README.md DESTINATION share/doc/cmake/03_install_file)
install(FILES ${lib_header_files} DESTINATION include)

# 3. 非目标文件的可执行程序安装(比如脚本之类) 
install(PROGRAMS rundemo.sh DESTINATION bin)

# 4. 目录的安装
install(DIRECTORY doc/ DESTINATION share/doc/cmake/03_install_file)

# 最终安装目录的结构如下：
# .
# ├── bin
# │   ├── app_demo.out
# │   └── rundemo.sh
# ├── include
# │   └── func_version.h
# ├── lib
# │   ├── libfunc_version.so -> libfunc_version.so.1
# │   ├── libfunc_version.so.1 -> libfunc_version.so.1.2
# │   └── libfunc_version.so.1.2
# ├── libstatic
# │   └── libfunc_version.a
# └── share
#     └── doc
#         └── cmake
#             └── 03_install_file
#                 ├── COPYRIGHT
#                 ├── README.md
#                 └── usage.md

# 8 directories, 10 files

# uninstall
# --------------------------------------------------
# 配置 uninstall 脚本
configure_file(
    "${CMAKE_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY
)

# 添加 uninstall 目标
add_custom_target(uninstall
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/cmake_uninstall.cmake
    COMMENT "Uninstalling..."
)
